package Model;

import java.sql.ResultSet;
import java.sql.Statement;



public class Role {
	
	
	   private DbConnection db = null;
	   private Statement st = null;
	   private ResultSet rs = null;   

	   
	   public Role(){
		   db = new DbConnection();	   
		   st = db.getStatement();
	   }

		public ResultSet getAllRole(){
			try{
				this.rs = this.st.executeQuery("Select * from role;");
			}catch (Exception e) {
				System.out.print(e.toString());
			}
			return this.rs;
		}
		
		public ResultSet getRoleById(int id){
			try{
				this.rs = this.st.executeQuery("SELECT * FROM role" +
						" WHERE id_role="+id+";");
			}catch (Exception e) {
				System.out.print(e.toString());
			}
			return this.rs;
		}
		
		public ResultSet getRoleByName(String name){
			try{
				this.rs = this.st.executeQuery("SELECT * FROM role" +
						" WHERE role_name='"+name+"';");
			}catch (Exception e) {
				System.out.print(e.toString());
			}
			return this.rs;
		}
		
		public int insertRole(String name){
			int result = -1;
			try{
				this.rs = this.st.executeQuery("INSERT INTO \"role\"("+
									            "role_name) VALUES ('"+name+"') returning id_role");
				this.rs.next();
				result = this.rs.getInt("id_role");
								
			}catch (Exception e) {
				System.out.print(e.toString());
				return result;
			}
			return result;
		}
		
		public int deleteRoleById(int id){
			int result = -1;
			try{
				this.rs = this.st.executeQuery("DELETE FROM role WHERE id_role="+id+" returning id_role");
				
				this.rs.next();
				result = this.rs.getInt("id_role");
				
			}catch (Exception e) {
				System.out.print(e.toString());
				return result;
			}
			return result;
		}
		
		public int deleteRoleByName(String name){
			int result = -1;
			try{
				this.rs = this.st.executeQuery("DELETE FROM role where role_name='"+name+"' returning id_role");
				this.rs.next();
				result = this.rs.getInt("id_role");
				
			}catch (Exception e) {
				System.out.print(e.toString());
				return result;
			}
			return result;
		}

	
	}
